define([],function(){
	function shoppingADD(){
		shoppingCar();
		add();
		reduce();
	}
	/******footer中商品的数量显示是根据缓存中shopingCarNum的数量而决定  为0则隐藏****/
	function shoppingCar(){
		$('#shop>span').text(localStorage.getItem('shoppingCarNum'));
		if($('#shop>span').text().trim() =='0'){
			$('#shop>span').css('display','none');
		}else{
			$('#shop>span').css('display','inline-block');
		}
	}
	/**点击+号,显示**/
	function add(){
		$('.add').on('click',function(){
			addOrReduce.call(this,'.add');
			Goods.call(this,'.add');
		})
	}
	function reduce(){
		$('.jian').on('click',function(){
			addOrReduce.call(this,'.jian');
			Goods.call(this,'.jian')
		})
	}
	function addOrReduce(or){
		var sum;
		var num = localStorage.getItem('shoppingCarNum');
		if(or == '.add'){
			sum = parseFloat($(this).prev().text());
			sum++;
			$(this).prev().addClass('op').text(sum).prev().addClass('op');
			num++;
		}else{
			sum = parseFloat($(this).next().text());
			sum--;
			num--;
			$(this).next().text(sum)
			if(sum == 0){
				$(this).removeClass('op');
				$(this).next().removeClass('op');
			}
		}
		localStorage.setItem('shoppingCarNum',num);
		shoppingCar();
	}
	
	function Goods(jiaOrjian){
		for(var i=0;i<$('.ul li span').length;i++){
			if($('.ul li span').eq(i).attr('class') == 'span'){
				var classify = $('.ul li').eq(i).text();
			}
		}
		var id = $(this).parent().parent().parent().attr('id'),
			price = $(this).parent().parent().find('.price_now').text().substring(1),
			count = $(this).prev().text(),
			classifyID = classify + id;
		if(!localStorage.getItem(classifyID)){
			localStorage.setItem(classifyID,classifyID);
			var goods = {
				img : $(this).parent().parent().prev().find('img').attr('src'),
				name : $(this).parent().parent().find('.shuiguo_name').text(),
				price : price,
				Id : id,
				classify : classify,
				count : count,
			},
			pro = {
				product : []
			};
			if(!localStorage.getItem('shop_goods')){
				pro.product.push(goods);
				localStorage.setItem('shop_goods',JSON.stringify(pro));
			}else{
				var data = JSON.parse(localStorage.getItem('shop_goods'));
				data.product.push(goods);
				localStorage.setItem('shop_goods',JSON.stringify(data));
			}
		}else{
			var obj = JSON.parse(localStorage.getItem('shop_goods'));
			for(var i in obj.product){
				if(obj.product[i].Id == id && obj.product[i].classify == classify){
					var counts = parseInt(obj.product[i].count);
					if(jiaOrjian == '.add'){
						counts++;
					}else{
						counts--;
						if(counts == 0){
							var cla = obj.product[i].classify + obj.product[i].Id;
							localStorage.removeItem(cla);
							obj.product.splice(i,1);
							localStorage.setItem('shop_goods',JSON.stringify(obj));
							return;
						}
					}
					obj.product[i].count = counts;
					localStorage.setItem('shop_goods',JSON.stringify(obj));
				}
			}
		}
	}
	
	return {
		shoppingADD : shoppingADD
	}
})